package com.demo.user.util.page;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.CaseFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.validation.constraints.Positive;

/**
 * @author xiexiaohu
 */
@ToString
@ApiModel(value = "分页请求数据")
public class PageParam {
    @ApiModelProperty(value = "单页显示数量，默认是10")
    @Positive
    private int pageSize;
    @ApiModelProperty(value = "页码，从1开始数，默认是1")
    @Positive
    private int pageNo;
    @ApiModelProperty(value = "", hidden = true)
    private int pageStart;
    @ApiModelProperty(value = "排序字段,例如：createdAt")
    private String orderBy;
    @ApiModelProperty(value = "升降序规则,ASC或DESC")
    private String order;

    public int getPageSize() {
        if (pageSize == 0) {
            return 10;
        }
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNo() {
        if (pageNo == 0) {
            return 1;
        }
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageStart() {
        return pageStart;
    }

    public void setPageStart(int pageStart) {
        this.pageStart = pageStart;
    }

    public String getOrderBy() {
        return orderBy;
    }


    public String getFullOrderBy() {
        if (StringUtils.isBlank(orderBy)) {
            return null;
        }
        if (StringUtils.isBlank(order)) {
            return null;
        }
        return orderBy + " " + order;
    }

    public void setOrderBy(String orderBy) {
        String afterFilterOrderBy = OrderByUtil.filterOrderBy(orderBy);
        String orderByColumnName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, afterFilterOrderBy);
        this.orderBy = orderByColumnName;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        String afterFilterOrderType = OrderByUtil.filterOrderType(order);
        this.order = afterFilterOrderType;
    }

    public String getSortClause(String defaultSortClause, boolean keepFormat) {
        if (hasSortClause()) {
            if (!keepFormat) {
                String orderByColumnName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, orderBy);
                return orderByColumnName + " " + order;
            } else {
                return orderBy + " " + order;
            }
        } else {
            return defaultSortClause;
        }
    }

    public boolean hasSortClause() {
        return StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order);
    }

    public void preparePaging(String defaultSotClause, boolean isOffSet) {
        preparePaging(defaultSotClause, false, isOffSet);
    }

    public void preparePaging(String defaultSotClause, boolean keepFormat, boolean isOffSet) {
        String sortClause = this.getSortClause(defaultSotClause, keepFormat);
        if (isOffSet) {
            Page<Object> page = PageHelper.offsetPage(this.getPageStart(), this.getPageSize());
            page.setOrderBy(sortClause);
        } else {
            PageHelper.startPage(this.getPageNo(), this.getPageSize(), sortClause);
        }
    }

    public void preparePaging() {
        String sortClause = this.getSortClause(null, false);
        PageHelper.startPage(this.getPageNo(), this.getPageSize(), sortClause);
    }

    public void preparePaging(String defaultSotClause, String extSortClause, boolean keepFormat) {
        String sortClause = this.getSortClause(defaultSotClause, keepFormat);
        if (extSortClause != null) {
            if (sortClause != null) {
                sortClause += ", " + extSortClause;
            } else {
                sortClause = extSortClause;
            }
        }
        PageHelper.startPage(this.getPageNo(), this.getPageSize(), sortClause);
    }

    public static PageParam getDefault() {
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(1);
        pageParam.setPageSize(Integer.MAX_VALUE);
        return pageParam;
    }

    public static PageParam getAll(PageParam fromPageParam) {
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(1);
        pageParam.setPageSize(Integer.MAX_VALUE);
        pageParam.setOrder(fromPageParam.getOrder());
        pageParam.setOrderBy(fromPageParam.getOrderBy());
        return pageParam;
    }
}
